
/*
HappyFlags_1.pde -- timers act directly on pins --first example with fixed speeds.

Copyright (c) 2010 Ed Bennett.  All rights reserved. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
// Three LED's blinking together at different rates.
// Three timer channels are used. The channels are 
// numbered 0, 1, and 2. 
//
// Timer commands usually take a channel number and/or a 
// time-value. 
//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29
//
// This example uses direct control of the pins from the timer channels.
// This method uses less processor power than callbacks or flag polling.
// (see the other examples)


#include <TimerScheduler.h>

// hardware pins
int HF0 = 13;
int HF1 = 12;
int HF2 = 11;
int HF3 = 10;
int HF4 = 9;

int outval0 = 0;
int outval1 = 0;
int outval2 = 0;
int outval3 = 0;
int outval4 = 0;


void setup()
{   
  pinMode(HF0, OUTPUT); 
  pinMode(HF1, OUTPUT); 
  pinMode(HF2, OUTPUT);
  pinMode(HF3, OUTPUT); 
  pinMode(HF4, OUTPUT); 
  
  Timer.begin();        // start the hardware timer
  
  
  
   
  // Register user's callback function name with a timer channel.
  // blinkLED0 is a function I wrote further down this page.
 
  Timer.onTick(0, doHappyFlag0); //(channelNumber, functionName)
  Timer.onTick(1, doHappyFlag1); 
  Timer.onTick(2, doHappyFlag2); 
  Timer.onTick(3, doHappyFlag3);
  Timer.onTick(4, doHappyFlag4);
  

  // time interval
  Timer.repeat(0,1501); // (channel, time)
  Timer.repeat(1,700); 
  Timer.repeat(2,300);
  Timer.repeat(3,1501); // (channel, time)
  Timer.repeat(4,700); 

   
  // attach LED pins to timer channels
  Timer.attachPin(0, HF0); // (channel, value)
  Timer.attachPin(1, HF1);
  Timer.attachPin(2, HF2); 
  Timer.attachPin(3, HF1);
  Timer.attachPin(4, HF2); 
  
  // using pin-direct mode, you need to set the pin-high
  // and pin-low times seperately
  Timer.setHighTime(0, 1500); // (channel, value)
  Timer.setLowTime(0, 500); 
  
  Timer.setHighTime(1,300);
  Timer.setLowTime(1, 1000);
  
  Timer.setHighTime(2, 700);
  Timer.setLowTime(2, 2100);
  
  Timer.setHighTime(2, 500);
  Timer.setLowTime(2, 200);
  
  Timer.setHighTime(3, 200);
  Timer.setLowTime(3, 50);
  
  Timer.setHighTime(4, 100);
  Timer.setLowTime(4, 100);
  
  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2); 
  Timer.start(3);       // (channel)
  Timer.start(4); 

}


// You can put things in loop(), if you like,
// but it's not required.

void loop()
{
}

// here are the user's callbacks

void doHappyFlag0(void){
    outval0 ^= 1;          // toggle the pin
    digitalWrite(HF0, outval0);
}

void doHappyFlag1(void){
    outval1 ^= 1;
    digitalWrite(HF1, outval1);
}

void doHappyFlag2(void){
    outval2 ^= 1;
    digitalWrite(HF2, outval2);
}

void doHappyFlag3(void){
    outval0 ^= 1;          // toggle the pin
    digitalWrite(HF0, outval0);
}

void doHappyFlag4(void){
    outval1 ^= 1;
    digitalWrite(HF1, outval1);
}
